
class Budget {
  createBudgets (hotels, stay) {
    this.hotels = hotels
    this.clientType = this.returnClientType(stay)
    this.days = this.returnDays(stay)
    this.totalDays = this.returnTotalDays()
    this.weekendDays = this.returnWeekendDays(this.days)
    this.weekDays = this.returnWeekDays()
    return this.returnPriceHotels(this.clientType)
  }

  returnWeekDays () {
    return this.totalDays - this.weekendDays
  }

  returnClientType (stay) {
    return stay.trim().split(':')[0].toLowerCase()
  }

  returnStay (stay) {
    return stay.trim().split(':')[1]
  }

  returnDays (stay) {
    return this.returnStay(stay).split(',')
  }

  returnTotalDays () {
    return this.days.length
  }

  returnWeekendDays (days) {
    return days.map(day => {
      if (this.isWeekend(day)) { return 1 }
      return 0
    }).reduce((dayPrevious, dayCurrent) => {
      return dayPrevious + dayCurrent
    })
  }

  returnPriceHotels (clientType) {
    return this.hotels.map(hotel => {
      return {
        name: hotel.getName(),
        rating: hotel.getRating(),
        budget: this.calculateBudget(clientType, hotel.getRate())
      }
    })
  }

  calculate (priceWeek, priceWeekend) {
    return (this.weekDays * priceWeek) + this.weekendDays * priceWeekend
  }

  calculateBudget (clientType, rate) {
    const priceWeek = rate.getPriceWeek()
    const priceWeekend = rate.getPriceWeekend()
    return this.calculatePrice(priceWeek, priceWeekend)
  }

  calculatePrice (priceWeek, priceWeekend) {
    if (this.isRegularClient(this.clientType)) {
      return this.calculate(priceWeek.getRegularPrice(), priceWeekend.getRegularPrice())
    }
    return this.calculate(priceWeek.getLoyaltyPrice(), priceWeekend.getLoyaltyPrice())
  }

  isWeekend (day) {
    const re = /(sat)|(sun)/
    return re.test(day)
  }

  isRegularClient (clientType) {
    return clientType === 'regular'
  }
}

module.exports = Budget
